<template>
  <div class="login-wrap">
    <div class="title">手机妈妈后台管理系统</div>
    <div class="bg">
      <div class="ms-login">
        <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="90px" class="demo-ruleForm ms-content"
          v-loading="loading" @submit.prevent @keyup.enter="submitForm(ruleFormRef, ruleForm, 'POST')">
          <el-form-item label="账号：" prop="username">
            <el-input v-model.trim="ruleForm.username" autocomplete="on"></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input type="password" v-model.trim="ruleForm.password" autocomplete="on"></el-input>
          </el-form-item>
          <div class="login-btn">
            <el-button type="primary" :icon="disabledbtn ? 'el-icon-loading' : ''" :disabled="disabledbtn"
              style="width: 100%" @click="submitForm(ruleFormRef, ruleForm, 'POST', 'postLogin')">{{ loadName }}
            </el-button>
          </div>
        </el-form>
      </div>
    </div>
    <div class="wzg-foot">
      <div class="wzg-foot-content">浙ICP备19029513号-1</div>
      <div class="wzg-foot-content">Copyright © 2021 义乌海烨电子有限公司</div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue';
import { useRouter } from 'vue-router';
import { api as Request } from '@/utils/api';
import { ElMessage } from 'element-plus';
const loading = ref(false);
const disabledbtn = ref(false);
const router = useRouter();
const ruleForm = reactive({
  username: '',
  password: ''
});
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};

const ruleFormRef = ref(null);

const loadName = computed(() => {
  return disabledbtn.value ? '登录中...' : '登录';
});

const submitForm = (formEl, formData, type, method) => {
  console.log(formEl);
  formEl.validate((valid) => {
    if (!valid) return false;
    handleLogin(formData, type, method);
  });
};

/* 登录 */
const handleLogin = (formData, type, method) => {
  disabledbtn.value = true;
  loading.value = true;
  Request[method](formData, type)
    .then((res) => {
      ElMessage.success('登录成功');
      localStorage.setItem('Authorization', res.result.accessToken);
      localStorage.setItem('refreshToken', res.result.refreshToken);
      console.log(router.currentRoute.value);
      const redirect = router.currentRoute.value.query.redirect;
      console.log(redirect);
      /* 重新登录返回原有的页面位置（如果存在的话） */
      if (redirect) {
        router.push(redirect);
      } else {
        router.push('/');
      }
    })
    .catch((error) => { console.log(error); }).finally(() => {
      disabledbtn.value = false;
      loading.value = false;
    });
};
</script>

<style scoped>
.title {
  position: absolute;
  left: 50%;
  top: 23%;
  font-size: 35px;
  text-align: center;
  color: #fff;
  transform: translateX(-50%);
}

.title-english {
  top: 25%;
  color: #777d8e;
  font-size: 14px;
}

.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background: url(http://img.zgwstxc.com/admin-bg.jpg) no-repeat;
  background-size: cover;
}

.ms-title {
  width: 100%;
  border-bottom: 1px solid #ddd;
  text-align: center;
  font-size: 20px;
  color: #fff;
  line-height: 50px;
}

.bg {
  position: absolute;
  left: 50%;
  top: 31%;
  width: 400px;
  margin-top: 10px;
  background-color: #409eff;
  border-radius: 5px;
  transform: translateX(-50%);
}

.ms-login {
  margin: 10px;
  background: rgba(255, 255, 255, 0.9);
}

.ms-content {
  padding: 30px 30px 30px 25px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 30%;
  height: 36px;
  margin-top: 10px;
  background-color: #4c9eff;
  font-size: 14px;
}

.login-tips {
  font-size: 12px;
}

.lf {
  float: left;
}

.box {
  min-width: 350px;
  margin-left: 50px;
  width: 30%;
}

.rf {
  float: right;
}

.clearfix:after {
  content: '.';
  display: block;
  height: 0;
  visibility: hidden;
  clear: both;
}

.clearfix {
  *zoom: 1;
}

.wzg-foot {
  position: absolute;
  left: 50%;
  bottom: 0;
  margin: 48px 0 24px 0;
  text-align: center;
  transform: translateX(-50%);
}

.wzg-foot-bottom {
  margin-bottom: 5px;
}

.wzg-foot .wzg-foot-content {
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

.wzg-foot .wzg-foot-content span {
  color: rgba(255, 255, 255, 0.9);
}

.captcha_img {
  margin-bottom: 20px;
  text-align: left;
  cursor: pointer;
}

.captcha_img img {
  width: 90px;
  height: 32px;
  vertical-align: middle;
}
</style>
<style>
.el-input__inner {
  box-shadow: 0 0 0 1000px white inset !important;
}
</style>